package models

import (
	"errors"
	"strconv"
)

//作业03_封装一个四则运算的计算器
func FourFun(a interface{}, b interface{}, str string) (float64, error) {
	var res float64
	var err error
	af := SetType(a)
	bf := SetType(b)
	switch str {
	case "+":
		res = af + bf
		break
	case "-":
		res = af - bf
		break
	case "*":
		res = af * bf
		break
	case "/":
		if af != 0 {
			res = af / bf
		} else {
			err = errors.New("除数不能为空")
		}
		break
	}
	return res, err
}

//对参数格式进行校验
func SetType(inter interface{}) float64 {
	var res float64
	switch inter.(type) {
	case string:
		strconv.ParseFloat(inter.(string), 64)
		break
	case int:
		res = float64(inter.(int))
		break
	case float64:
		res = inter.(float64)
		break
	}
	return res
}
